package com.dxyxx.framework;

import org.apache.http.Header;
import org.codehaus.jackson.type.JavaType;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.pierry.simpletoast.SimpleToast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import com.android.customized.CountdownButtonHandler;
import com.dxyxx.R;
import com.dxyxx.dialog.AgainPlayer;
import com.dxyxx.dialog.WaittingCircle;
import com.dxyxx.jni.AESUtil;
import com.dxyxx.jni.SecurityUtil;
import com.dxyxx.pojo.ResponseResult;
import com.dxyxx.pojo.User;
import com.dxyxx.utils.PhoneValidator;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ta.utdid2.android.utils.StringUtils;

public class MobGetbackPasswordActivity extends BaseActivity {
	private CountdownButtonHandler handler;
	private Button codeButton;
	private Button resetButton;
	private EventHandler eh;
	private EditText phone;
	private EditText code;
	private EditText password;
	private Context context;
	private EditText repeatPassword;
	private ProgressDialog progressDialog;//全局等待框

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_getbackpassword);
		phone = (EditText) findViewById(R.id.username);
		phone.setSelectAllOnFocus(true);
		phone.setFocusable(true);
		phone.setFocusableInTouchMode(true);

		code = (EditText) findViewById(R.id.code);
		code.setSelectAllOnFocus(true);
		code.setFocusable(true);
		code.setFocusableInTouchMode(true);

		password = (EditText) findViewById(R.id.password);
		password.setSelectAllOnFocus(true);
		password.setFocusable(true);
		password.setFocusableInTouchMode(true);

		repeatPassword = (EditText) findViewById(R.id.repeatPassword);
		repeatPassword.setSelectAllOnFocus(true);
		repeatPassword.setFocusable(true);
		repeatPassword.setFocusableInTouchMode(true);

		initViews();

		handler = new CountdownButtonHandler(MobGetbackPasswordActivity.this, codeButton);
		eh = new EventHandler() {

			@Override
			public void afterEvent(int event, int result, Object data) {

				if (result == SMSSDK.RESULT_COMPLETE) {
					//回调完成
					if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
						//提交验证码成功

					} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
						//获取验证码成功
						// 使用 handler。。 
						handler.sendEmptyMessage(0);
					} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
						//返回支持发送验证码的国家列表
					}
				} else {
					((Throwable) data).printStackTrace();
				}
				SMSSDK.unregisterEventHandler(this);
			}
		};
	}

	private void initViews() {
		context = this;
		((ImageView) findViewById(R.id.backImageButton)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		((ImageView) findViewById(R.id.nextImageButton)).setVisibility(View.INVISIBLE);
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText("重置密码");
		titleView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		resetButton = (Button) findViewById(R.id.resetButton);
		// 点击发送验证码。。等待30秒
		codeButton = (Button) findViewById(R.id.b_send);
		codeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判断手机号 是否填写正确
				String phoneStr = phone.getText().toString().trim();
				if (StringUtils.isEmpty(phoneStr)) {
					SimpleToast.error(context, "手机号不能为空");
					return;
				} else if (!PhoneValidator.isMobileNO(phoneStr)) {
					SimpleToast.error(context, "手机号错误");
					return;
				} else {
					SMSSDK.registerEventHandler(eh);
					//发送手机验证码
					SMSSDK.getVerificationCode("86", phoneStr.trim());
				}
			}
		});

		resetButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判断手机号 是否填写正确
				String phoneStr = phone.getText().toString().trim();
				String codeStr = code.getText().toString().trim();
				String passwordStr = password.getText().toString().trim();
				String repeatPasswdStr = repeatPassword.getText().toString().trim();
				if (StringUtils.isEmpty(phoneStr) || StringUtils.isEmpty(codeStr) || StringUtils.isEmpty(passwordStr) || StringUtils.isEmpty(repeatPasswdStr)) {
					SimpleToast.error(context, "手机号|验证码 不能为空");
					return;
				} else if (!repeatPasswdStr.equals(passwordStr)) {
					SimpleToast.error(context, "密码不一致");
					return;
				} else {
					// 发送http 请求
					String url = "mob/getback";
					RequestParams rp = new RequestParams();
					rp.put("phone", phoneStr);
					rp.put("code", codeStr);
//					rp.put("password", passwordStr);
					try {
						String key = new String(SecurityUtil.getKeyValue(MobGetbackPasswordActivity.this));
						String encryptPassword = AESUtil.Encrypt(passwordStr, key);
						rp.put("password", encryptPassword);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// 禁止 注册按钮点击了。
					resetButton.setClickable(false);
					// 启动等待框
					progressDialog = WaittingCircle.showDialog(context, false);

					HttpUtil.post(url, rp, new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							try {
								JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(ResponseResult.class, User.class);
								ResponseResult<User> rr = getObjectMapper().readValue(arg2, javaType);

								//{"object":null,"code":1,"info":"验证码已发送，稍等"}
								if (rr.getCode() == 1) { //成功
									// 显示信息
									SimpleToast.ok(context, "重置密码成功");

									// 关闭Activity.直接回到上一页
									MobGetbackPasswordActivity.this.finish();
								} else { //失败 
									// 显示信息
									SimpleToast.info(context, rr.getInfo());
									resetButton.setClickable(true);//激活 按钮
								}
							} catch (Exception e) {
								e.printStackTrace();
								SimpleToast.error(context, "系统内部出错！");
								resetButton.setClickable(true);//激活 按钮
							}
							progressDialog.dismiss();
						}

						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
							AgainPlayer.makeCall((Activity) context, "网络好像出错了");
							resetButton.setClickable(true);//激活 按钮
							progressDialog.dismiss();
						}
					});
				}

			}
		});

	}
}
